import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { User } from '../api'

export const useUserStore = defineStore('user', () => {
    const isLoggedIn = ref(false)
    const userInfo = ref<User | null>(null)

    const login = (user: User) => {
        isLoggedIn.value = true
        userInfo.value = user
        localStorage.setItem('user', JSON.stringify(user))
    }

    const logout = () => {
        isLoggedIn.value = false
        userInfo.value = null
        localStorage.removeItem('user')
    }

    // 检查本地存储是否有用户信息
    const checkAuth = () => {
        const userData = localStorage.getItem('user')
        if (userData) {
            userInfo.value = JSON.parse(userData)
            isLoggedIn.value = true
        }
    }

    return { isLoggedIn, userInfo, login, logout, checkAuth }
})